/*
 */

package com.railway.news.dao.impl;
import com.railway.news.dao.INewsDao;
import org.springframework.stereotype.Repository;

import java.util.*;
import org.apache.commons.lang.StringUtils;

import com.railway.common.base.*;
import com.railway.common.util.*;

import cn.org.rapid_framework.util.*;
import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;
import cn.org.rapid_framework.page.impl.*;

import com.railway.news.model.*;
import com.railway.news.dao.*;
import com.railway.news.service.*;
import com.railway.news.util.NewsCfgManager;
import com.railway.news.util.XMLContentHelper;
import com.railway.news.vo.query.*;

/**
 * @author  jiangcheng
 * @version 1.0
 * @since 1.0
 */




@Repository("newsDao")
public class NewsDaoImpl extends BaseIbatisDao<News,java.lang.String> implements INewsDao{
	
	@Override
	public String getIbatisSqlMapNamespace() {
		return "News";
	}
	
	public News getById(java.lang.String id){
		News n = super.getById( id);
		try {
			String context = XMLContentHelper.queryContent(NewsCfgManager.getNewsPubDir()+n.getContentHtml(),id);
		//	context = context.replaceAll("<", "&lt;");
			//context = context.replaceAll(">", "&gt;");
			n.setContentText(context);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 
		return n;
	}
	
	public void deleteById(java.lang.String id){
		super.deleteById(id);
	}
	
	/** 插入数据 */
	public void save(News entity){
		if(StringUtils.isEmpty(entity.getId())) {
			entity.setId(IdGenerator.genUUIDStr());
		}
		super.save(entity);
	}
	
	/** 更新数据 */
	public void update(News entity){

		String content = entity.getContentText();
		entity.setContentText("");
		super.update(entity);
		try {
	
			XMLContentHelper.modifyContent(entity.getFromUrl(), entity.getId(),content);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

	public boolean isUnique(News entity, String uniquePropertyNames){
		return super.isUnique( entity, uniquePropertyNames);
	}
	
	public List<News> findAll(){
		return super.findAll();
	}
	
	public void saveOrUpdate(News entity) {
		if(StringUtils.isEmpty(entity.getId())) {
			entity.setId(IdGenerator.genUUIDStr());
			super.save(entity);
		}
		else 
			super.update(entity);
	}
	public Page findPageLike(NewsQuery query) {
		Page r = pageQuery("News.findPageLike",query);
		List list = r.getResult();
		if(list != null){
			for(int i = 0  ; i < list.size();i++){
				News n = (News)list.get(i);
				String id = n.getId();
				try {
				n.setContentText("");//别显示内容,否则出错
				n.setAuthorId(n.getAuthorId() == null? "":StaticMap.getUserValue(n.getAuthorId()));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		r.setResult(list);
		return r;
	}
	public Page findPage(NewsQuery query) {
		Page r = pageQuery("News.findPage",query);
		List list = r.getResult();
		if(list != null){
			for(int i = 0  ; i < list.size();i++){
				News n = (News)list.get(i);
				String id = n.getId();
				try {
				n.setContentText("");//别显示内容,否则出错
				n.setAuthorId(n.getAuthorId() == null? "":StaticMap.getUserValue(n.getAuthorId()));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		r.setResult(list);
		return r;
	}
	
	public List<News> findByTitle(String title){
		Map map=new HashMap();
		map.put("title", title);
		return this.getSqlMapClientTemplate().queryForList("News.findbyTitle", map);
	}
	
}
